package com.yidu.hbatsams.service.Impl;

import com.yidu.hbatsams.action.emp.EmpWordAddData;
import com.yidu.hbatsams.domain.*;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.service.emp.*;
import com.yidu.hbatsams.utils.Tools;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @program: hbats
 * @description: 生成word文件流
 * @author: 张超
 * @create: 2019-12-02 15:20
 */

@Service
public class AtsEmpWordServicImpl implements AtsEmpWordServic {
    @Resource
    AtsEmpAthleteResultsService atsEmpAthleteResultsService;//比赛信息
    @Resource
    AtsEmpAthleteService atsEmpAthleteService;//运动员
    @Resource
    AtsEmpCoachService atsEmpCoachService;//教练员
    @Resource
    AtsEmpExpertService atsEmpExpertService;//专家
    @Resource
    AtsEmpLeaderService atsEmpLeaderService;//中心领导
    @Resource
    AtsDataDicSerivceImpl atsDataDicSerivce;//数据字典
    @Resource
    AtsBaseSportService atsBaseSportService;//运动项目


    @Override
    public XWPFDocument createWord(int type, int id, HttpServletResponse response, User user,String contextPath, HttpSession session) throws IOException {

        EmpWordAddData empWordAddData = new EmpWordAddData();

        XWPFDocument document = null;

       if(type==1){//如果是运动员
           AtsEmpAthlete atsEmpAthlete = atsEmpAthleteService.findById(id);

           if(atsEmpAthlete==null){
               return null;
           }else{
               atsEmpAthlete.setCreater(user.getUserName());
               atsEmpAthlete.setCreatTime(new Date());
               response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("运动员："+atsEmpAthlete.getAthleteName(),"UTF-8")+".docx");
               if(atsEmpAthlete!=null){
                   if(atsEmpAthlete.getCoachId()!=null){//如果存在教练员  就要把名字查出来
                       atsEmpAthlete.setCoachName(atsEmpCoachService.findById(atsEmpAthlete.getCoachId()).getCoachName());
                   }

                   //////取出数据字典名字
                   if(!Tools.isEmpty(atsEmpAthlete.getLevel())){
                       String level = atsDataDicSerivce.findById(Integer.valueOf(atsEmpAthlete.getLevel())).getDname();
                       if(!Tools.isEmpty(level)){
                           atsEmpAthlete.setLevel(level);
                       }
                   }

                   if(!Tools.isEmpty(atsEmpAthlete.getEducation())){
                       String education = atsDataDicSerivce.findById(Integer.valueOf(atsEmpAthlete.getEducation())).getDname();
                       if(!Tools.isEmpty(education)){
                           atsEmpAthlete.setEducation(education);
                       }
                   }

                   if(!Tools.isEmpty(atsEmpAthlete.getBestResult())){
                       String bestResult = atsDataDicSerivce.findById(Integer.valueOf(atsEmpAthlete.getBestResult())).getDname();
                       if(!Tools.isEmpty(bestResult)){
                           atsEmpAthlete.setBestResult(bestResult);
                       }
                   }
               }
           }
           document = empWordAddData.exportWord(atsEmpAthlete,type,contextPath,session);
        }else if(type==2){//如果是教练员
           AtsEmpCoach atsEmpCoach = atsEmpCoachService.findById(id);

           if(atsEmpCoach==null){
               return null;
           }

           atsEmpCoach.setCreater(user.getUserName());
           atsEmpCoach.setCreatTime(new Date());

           response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("教练员："+atsEmpCoach.getCoachName(),"UTF-8")+".docx");
           if(!Tools.isEmpty(atsEmpCoach.getExpertSpecia())){//如果存在运动项目ID 就把名字查出来
               atsEmpCoach.setExpertSpecia(atsBaseSportService.findById(Integer.valueOf(atsEmpCoach.getExpertSpecia())).getSportName());
           }
           document = empWordAddData.exportWord(atsEmpCoach,type,contextPath,session);
       }else if(type==3){
           AtsEmpExpert atsEmpExpert = atsEmpExpertService.selectByPrimaryKey(id);
           if(atsEmpExpert==null){
               return null;
           }

           atsEmpExpert.setCreater(user.getUserName());
           atsEmpExpert.setCreatTime(new Date());

           response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("专家："+atsEmpExpert.getExpertName(),"UTF-8")+".docx");
           if(!Tools.isEmpty(atsEmpExpert.getExpertSpecia())){//如果存在运动项目ID 就把名字查出来
               atsEmpExpert.setExpertSpecia(atsBaseSportService.findById(Integer.valueOf(atsEmpExpert.getExpertSpecia())).getSportName());
           }
           document = empWordAddData.exportWord(atsEmpExpert,type,contextPath,session);
       }else if(type==4){
           AtsEmpLeader atsEmpLeader = atsEmpLeaderService.selectByPrimaryKey(id);
           if(atsEmpLeader==null){
               return null;
           }
           atsEmpLeader.setCreater(user.getUserName());
           atsEmpLeader.setCreatTime(new Date());
           response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("领导："+atsEmpLeader.getLeaderName(),"UTF-8")+".docx");
           document = empWordAddData.exportWord(atsEmpLeader,type,contextPath,session);
       }else if(type==5){
           AtsEmpAthleteResults atsEmpAthleteResults = atsEmpAthleteResultsService.selectByPrimaryKey(id);
           if(atsEmpAthleteResults==null){
               return null;
           }
           atsEmpAthleteResults.setCreater(user.getUserName());
           atsEmpAthleteResults.setCreatTime(new Date());
           if(atsEmpAthleteResults.getAthleteId()!=null){
               atsEmpAthleteResults.setAthleteName(atsEmpAthleteService.findById(atsEmpAthleteResults.getAthleteId()).getAthleteName());
           }
           response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("比赛成绩："+atsEmpAthleteResults.getAthleteName(),"UTF-8")+".docx");
           document = empWordAddData.exportWord(atsEmpAthleteResults,type,contextPath,session);
       }
       return document;
    }

}